import { Toast } from "antd-mobile";
import { reqDetailInfo,reqAddShop } from "../../http/api";

// 1.初始值
const initState = {
    // 1.初始化数组
    list: {}
}

// types
const types = {
    // 1.2
    DETAIL_CHANGE_LIST: "DETAIL_CHANGE_LIST"
}
// reducer
export const detailReducer = (state = initState, action) => {
    switch (action.type) {
        // 1.3
        case types.DETAIL_CHANGE_LIST:
            return {
                ...state,
                list: action.list
            }
            break;
        default:
            return state
    }
}
// actions
export const actions = {
    // 1.4
    changeList: list => ({ type: types.DETAIL_CHANGE_LIST, list }),
    // 1.6 发请求
    reqDetailAction: (params) => {
        return (dispatch, getState) => {
            // 1.7 发请求
            reqDetailInfo(params).then(res => {
                if (res.data.code === 200) {
                    //1.8 赋值修改
                    dispatch(actions.changeList(res.data.list[0]))
                }
            })
        }
    },
    // 1.9 加入购物车
    reqAddShopAction: (params) => {
        return (dispatch, getState) => {
            //发请求
            reqAddShop(params).then(res => {
                // 判断是否成功
                if (res.data.code === 200) {
                    // 弹成功
                    Toast.success(res.data.msg)
                }
            })
        }
    }
}


// 导出数据
// 1.5 
export const getList = state => state.detail.list